//
//  NdoeliveryViewController.m
//  ZLShang
//
//  Created by wangguangming on 15/8/11.
//  Copyright © 2015年 Amber. All rights reserved.
//

#import "NdoeliveryViewController.h"
#import "ZLShang.h"
#import "Base.h"
#import "ReadyDiSignle.h"
#import "SendTableViewCell.h"
#import "SendDetailViewController.h"
@interface NdoeliveryViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *mainTable;

@end

@implementation NdoeliveryViewController
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [[ReadyDiSignle signle] getnoDeliveryWorking];
    [[ReadyDiSignle signle] addObserver:self forKeyPath:@"NodeliveArray" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [[ReadyDiSignle signle] removeObserver:self forKeyPath:@"NodeliveArray"];

    
    [super viewWillDisappear:animated];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
        {
        self.view.backgroundColor=[UIColor whiteColor];
        }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setBack];
    
    self.title=@"未完成包裹";
    self.mainTable=[[UITableView alloc] init];
    
    self.mainTable.frame=CGRectMake(0, 0, WIDTH, HEIGHT);
    self.mainTable.delegate=self;
    self.mainTable.dataSource=self;
    
    self.mainTable.separatorStyle = UITableViewCellSelectionStyleNone;
    [self.view addSubview:self.mainTable];
    
        // Do any additional setup after loading the view.
}
-(void)observeValueForKeyPath:(nullable NSString *)keyPath ofObject:(nullable id)object change:(nullable NSDictionary *)change context:(nullable void *)context
{
    if ([keyPath isEqualToString:@"NodeliveArray"]) 
        {
        
        
            // NSLog(@"%@",[ReadyDiSignle signle].NodeliveArray);
        
        [self.mainTable reloadData];
               }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [ReadyDiSignle signle].NodeliveArray.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellid=@"sendcellid";
    
    Delivery *delivery=[ReadyDiSignle signle].NodeliveArray[indexPath.row];
    
    SendTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellid];
    if (cell==nil)
        {
        cell=[[SendTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
        }
    if (delivery.completed.intValue==0) 
        {
        cell.typeLaber.text=@"正在配送";
        cell.backgroundColor=[UIColor greenColor];
        }
    else
        {
        cell.typeLaber.text=@"配送完成";
        cell.backgroundColor=[UIColor redColor];
        }
    cell.senderLaber.text=[NSString stringWithFormat:@"送货员:%@(%@)",delivery.senderName,delivery.senderAcc];
    NSString *s;
    NSString *ss=@"";
    if (delivery.orders.count!=0)
        {
        for (int i=0; i<delivery.orders.count; i++)
            {
            
            Order *order=[[Order alloc] init];
            [order setValuesForKeysWithDictionary:delivery.orders[i]];
                // NSLog(@"===%@",order.oid);
            ss= [ss stringByAppendingString:[NSString stringWithFormat:@"%@,",[order.oid substringFromIndex:order.oid.length-3]]];
            }
        
        
        }
    
        //    NSDate *date1=[NSDate dateWithTimeIntervalSince1970:delivery.sentTime.intValue];
        //    NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];
    
    NSString *str=[NSString stringWithFormat:@"%lld",delivery.sentTime.longLongValue/1000];//开始时间戳
    NSTimeInterval time=[str doubleValue];//因为时差问题要加8小时 == 28800 sec
    NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
    cell.beginLaber.text=[NSString stringWithFormat:@"发货时间:%@",currentDateStr];
    
    if(delivery.completeTime!=nil)
        {
        NSString *str1=[NSString stringWithFormat:@"%lld",delivery.completeTime.longLongValue/1000];//结束时间戳
        NSTimeInterval time1=[str1 doubleValue];//因为时差问题要加8小时 == 28800 sec
        NSDate *detaildate1=[NSDate dateWithTimeIntervalSince1970:time1];
        
        NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
            //设定时间格式,这里可以设置成自己需要的格式
        [dateFormatter1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSString *currentDateStr1 = [dateFormatter stringFromDate: detaildate1];
        cell.endLaber.text=[NSString stringWithFormat:@"送达时间:%@",currentDateStr1];
        }
    else
        {
        cell.endLaber.text=[NSString stringWithFormat:@"送达时间:暂未送达"];
        }
        //实例化一个NSDateFormatter对象
    
    
    [ss substringToIndex:ss.length-1];
    cell.itemLaber.text=[NSString stringWithFormat:@"订单号:%@",[ss substringToIndex:ss.length-1]];
    cell.numberLaber.text=[NSString stringWithFormat:@"订单个数:%d个",delivery.orders.count];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    cell.layer.borderColor=[[UIColor whiteColor] CGColor];
    cell.layer.borderWidth=2.0;
    
    
    return cell;
}
-(CGFloat)tableView:(nonnull UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    return 200;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}
-(void)tableView:(nonnull UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    Delivery *delivery=[ReadyDiSignle signle].NodeliveArray[indexPath.row];
    
    SendDetailViewController *sendDetailViewController=[[SendDetailViewController alloc] init];
    sendDetailViewController.delivery=delivery;
    [self.navigationController pushViewController:sendDetailViewController animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
